﻿/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace DalmatianBuild.Test
{
    [TestClass]
    public class SvnTests
    {
        Svn svn;
        [TestInitialize]
        public void Initialize()
        {
            svn = new Svn();

            if (string.IsNullOrEmpty(svn.SvnExePath))
                Assert.Inconclusive("Unable to find svn.exe. Unable to perform SVN tests");

            if (string.IsNullOrEmpty(svn.SvnAdminExePath))
                Assert.Inconclusive("Unable to find svnadmin.exe. Unable to perform SVN tests");
        }

        [TestMethod]
        public void PathToUrlTest()
        {
            Svn svn = new Svn();
            Assert.AreEqual(@"file:///C:/Temp", svn.PathToUrl(@"C:\Temp"));
            Assert.IsTrue(svn.PathToUrl("test").Replace(@"/", @"\").Contains(Environment.CurrentDirectory));
        }
        [TestMethod]
        public void PathToUrlExceptions()
        {
            AssertB.ThrowsException<ArgumentException>(() => svn.PathToUrl(null));
        }

        [TestMethod]
        public void UrlAppendTest()
        {
            Svn svn = new Svn();
            Assert.AreEqual(@"part1/part2/part3", svn.UrlAppend("part1", "part2", "part3"));
            Assert.AreEqual(@"part1/part2/part3", svn.UrlAppend("part1/", "part2/", "part3/"));
        }
        [TestMethod]
        public void UrlAppendExceptions()
        {
            AssertB.ThrowsException<ArgumentException>(() => svn.UrlAppend(null));
            AssertB.ThrowsException<ArgumentException>(() => svn.UrlAppend("test", null));
            AssertB.ThrowsException<ArgumentException>(() => svn.UrlAppend("test", "test1", null));
        }

        [TestMethod]
        public void CreateRepositoryTest()
        {
            Svn svn = new Svn();
            var repositoryDirectory = Path.GetRandomFileName().Replace(".", "");
            Directory.CreateDirectory(repositoryDirectory);
            Assert.IsFalse(Directory.GetFiles(repositoryDirectory).Any());

            svn.CreateRepository(repositoryDirectory);

            Assert.IsTrue(Directory.GetFiles(repositoryDirectory).Any());
        }
        [TestMethod]
        public void CreateRepositoryExceptions()
        {
            AssertB.ThrowsException<ArgumentException>(() => svn.CreateRepository(null));

            svn.SvnAdminExePath = null;
            AssertB.ThrowsException<FileNotFoundException>(() => svn.CreateRepository("test"));

            svn.SvnAdminExePath = "badpath.exe";
            AssertB.ThrowsException<FileNotFoundException>(() => svn.CreateRepository("test"));
        }

        [TestMethod]
        public void ImportTest()
        {
            Svn svn = new Svn();
            var repositoryDirectory = Path.GetRandomFileName().Replace(".", "");
            var repositoryUrl = svn.PathToUrl(repositoryDirectory);
            Directory.CreateDirectory(repositoryDirectory);
            svn.CreateRepository(repositoryDirectory);

            var fileContent = Guid.NewGuid().ToString();
            var tempFile = Path.GetRandomFileName();
            File.WriteAllText(tempFile, fileContent);
            svn.Import(tempFile, svn.UrlAppend(repositoryUrl, tempFile), "test");
            File.Delete(tempFile);


            svn.Export(svn.UrlAppend(repositoryUrl, tempFile), tempFile);
            Assert.AreEqual(fileContent, File.ReadAllText(tempFile));
        }
        [TestMethod]
        public void ImportExceptions()
        {
            AssertB.ThrowsException<ArgumentException>(() => svn.Import(null, "destination", ""));
            AssertB.ThrowsException<ArgumentException>(() => svn.Import("repositoryUrl", null, ""));
            

            AssertB.ThrowsException<Exception>(() => svn.Import("invalidUrl", "destination", null));

            svn.SvnExePath = null;
            AssertB.ThrowsException<FileNotFoundException>(() => svn.Import("repositoryUrl", "destination", ""));

            svn.SvnExePath = "badpath.exe";
            AssertB.ThrowsException<FileNotFoundException>(() => svn.Import("repositoryUrl", "destination", ""));
        }


        [TestMethod]
        public void ExportTest()
        {
            ImportTest();
        }
        [TestMethod]
        public void ExportExceptions()
        {
            AssertB.ThrowsException<ArgumentException>(() => svn.Export(null, "destination"));
            AssertB.ThrowsException<ArgumentException>(() => svn.Export("repositoryUrl", null));

            AssertB.ThrowsException<Exception>(() => svn.Export("invalidUrl", "destination"));

            svn.SvnExePath = null;
            AssertB.ThrowsException<FileNotFoundException>(() => svn.Export("repositoryUrl", "destination"));

            svn.SvnExePath = "badpath.exe";
            AssertB.ThrowsException<FileNotFoundException>(() => svn.Export("repositoryUrl", "destination"));
        }
    }
}
